#include "../include/userCenterRpc.h"


message::resGetVarify * userCenterRpc::get_varify_code(std::string &email){
    //内部封装发送的信息
    grpc::ClientContext context;
    message::reqGetVarify req_msg;
    req_msg.set_email(email);
    message::resGetVarify *res_msg=new message::resGetVarify();
    grpc::Status status= _stub->GetVarifyCode(&context,req_msg,res_msg);
    if(!status.ok()){
        //如果返回错误
        res_msg->set_result(false);
    }
    return res_msg;
}

message::resGetVarify * userCenterRpc::get_varify_code(std::string &&email){
    //内部封装发送的信息
    grpc::ClientContext context;
    message::reqGetVarify req_msg;
    req_msg.set_email(email);
    message::resGetVarify *res_msg=new message::resGetVarify();
    grpc::Status status= _stub->GetVarifyCode(&context,req_msg,res_msg);
    if(!status.ok()){
        //如果返回错误
        res_msg->set_result(false);
        
    }
    return res_msg;
}


message::resRegisterUser *userCenterRpc::registerUser(std::string &userName, std::string &email, std::string &passwd, std::string &authCode)
{
    grpc::ClientContext context;
    message::reqRegisterUser req;
    req.set_username(userName);
    req.set_email(email);
    req.set_passwd(passwd);
    req.set_authcode(authCode);
    message::resRegisterUser *res=new message::resRegisterUser();
    grpc::Status sts=this->_stub->RegisterUser(&context,req,res);
    if(!sts.ok()){
        res->set_result(false);
        res->set_info("当前服务不可用！！");
    }
    return res;
}

message::resResetPasswd * userCenterRpc::reset_passwd(std::string &email,std::string &new_passwd,std::string &authCode){

    grpc::ClientContext context;
    message::reqResetPasswd req;
    req.set_email(email);
    req.set_new_passwd(new_passwd);
    req.set_authcode(authCode);
    message::resResetPasswd *res=new message::resResetPasswd();
    grpc::Status sts=this->_stub->ResetPasswd(&context,req,res);
    //如果服务端不在线，那么这个返回的状态码可能是grpc::StatusCode::UNAVAILABLE
    if(!sts.ok()){
        res->set_result(false);
        res->set_info("当前服务不可用！！");
    }
    return res;

}


message::resLogin *userCenterRpc::login(std::string &email, std::string &passwd)
{
    grpc::ClientContext context;
    message::reqLogin request;
    request.set_email(email);
    request.set_passwd(passwd);
    message::resLogin *res=new message::resLogin();
    grpc::Status sts=this->_stub->Login(&context,request,res);
    if(!sts.ok()){
        res->set_result(false);
        res->set_info("当前服务不可用");
    }

    return res;
}
